package org.ix.dynamicapis.controller;

import org.ix.dynamicapis.annotation.SetFieldTypes;
import org.ix.dynamicapis.model.PageList;
import org.ix.dynamicapis.utils.ExcelUtils;
import org.ix.dynamicapis.model.request.ApiSystemAddRequest;
import org.ix.dynamicapis.model.request.ApiSystemPatchRequest;
import org.ix.dynamicapis.model.request.ApiSystemQueryRequest;
import org.ix.dynamicapis.model.request.ApiSystemSaveRequest;
import org.ix.dynamicapis.model.response.ApiSystemDetailResponse;
import org.ix.dynamicapis.model.response.ApiSystemListItemResponse;
import org.ix.dynamicapis.service.ApiSystemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import java.io.IOException;
import java.net.URI;
import java.util.List;

@Api(tags = {"接口业务系统相关接口"})
@CrossOrigin
@RequestMapping("/api-system")
@RestController
@Validated
public class ApiSystemController {

    private final ApiSystemService apiSystemService;

    @Autowired
    public ApiSystemController(ApiSystemService apiSystemService) {
        this.apiSystemService = apiSystemService;
    }

    @ApiOperation(value = "添加接口业务系统", notes = "添加接口业务系统")
    @ApiImplicitParam(name = "apiSystem", value = "添加接口业务系统请求实体", required = true, dataType = "ApiSystemAddRequest", paramType = "body")
    @PostMapping("/add")
    public ResponseEntity<ApiSystemDetailResponse> add(@Validated @RequestBody @SetFieldTypes(types = {"create"}) ApiSystemAddRequest apiSystem) {
        ApiSystemDetailResponse detail = apiSystemService.add(apiSystem);
        return ResponseEntity.created(URI.create("")).body(detail);
    }

    @ApiOperation(value = "删除接口业务系统", notes = "删除接口业务系统")
    @DeleteMapping("/delete")
    public ResponseEntity<Integer> delete(@Validated @SetFieldTypes(types = {"update"}) ApiSystemPatchRequest patchRequest) {
        Integer count = apiSystemService.delete(patchRequest);
        return ResponseEntity.ok(count);
    }

    @ApiOperation(value = "禁用接口业务系统", notes = "禁用接口业务系统")
    @PatchMapping("/disable")
    public ResponseEntity<Integer> disable(@Validated @SetFieldTypes(types = {"update"}) ApiSystemPatchRequest patchRequest) {
        Integer count = apiSystemService.disable(patchRequest);
        return ResponseEntity.ok(count);
    }

    @ApiOperation(value = "启用接口业务系统", notes = "启用接口业务系统")
    @PatchMapping("/enable")
    public ResponseEntity<Integer> enable(@Validated @SetFieldTypes(types = {"update"}) ApiSystemPatchRequest patchRequest) {
        Integer count = apiSystemService.enable(patchRequest);
        return ResponseEntity.ok(count);
    }

    @ApiOperation(value = "根据接口ID更新接口业务系统", notes = "根据接口ID更新接口业务系统")
    @ApiImplicitParam(name = "apiSystem", value = "更新接口业务系统请求实体", required = true, dataType = "ApiSystemSaveRequest", paramType = "body")
    @PatchMapping("/update")
    public ResponseEntity<ApiSystemDetailResponse> updateById(@Validated @RequestBody @SetFieldTypes(types = {"update"}) ApiSystemSaveRequest apiSystem) {
        ApiSystemDetailResponse detail = apiSystemService.updateById(apiSystem);
        return ResponseEntity.ok(detail);
    }

    @ApiOperation(value = "根据接口ID获取接口业务系统详情", notes = "根据接口ID获取接口业务系统详情")
    @ApiImplicitParam(name = "id", value = "接口ID", required = true, dataType = "int", paramType = "query")
    @GetMapping("/detail")
    public ResponseEntity<ApiSystemDetailResponse> getById(@RequestParam("id") @NotNull(message = "接口ID不能为空") @Min(value = 1, message = "接口ID必须大于0") Integer id) {
        ApiSystemDetailResponse detail = apiSystemService.getById(id);
        return ResponseEntity.ok(detail);
    }

    @ApiOperation(value = "根据条件获取接口业务系统列表", notes = "根据条件获取接口业务系统列表")
    @GetMapping("/list")
    public ResponseEntity<List<ApiSystemListItemResponse>> getList(ApiSystemQueryRequest queryRequest) {
        return ResponseEntity.ok(apiSystemService.getList(queryRequest));
    }

    @ApiOperation(value = "根据条件分页获取接口业务系统列表", notes = "根据条件分页获取接口业务系统列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", dataType = "int", paramType = "query")
    })
    @GetMapping("/page-list")
    public ResponseEntity<PageList<ApiSystemListItemResponse>> getPageList(
            ApiSystemQueryRequest queryRequest,
            @RequestParam(value = "pageNum", defaultValue = "1") @Min(value = 1, message = "页码必须大于0") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "50") @Min(value = 1, message = "分页大小必须大于0") Integer pageSize
    ) {
        return ResponseEntity.ok(apiSystemService.getPageList(queryRequest, pageNum, pageSize));
    }

    @ApiOperation(value = "根据条件导出接口业务系统列表", notes = "根据条件导出接口业务系统列表")
    @ApiImplicitParam(name = "exportName", value = "导出文件名", dataType = "String", paramType = "query")
    @GetMapping("/export")
    public ResponseEntity<?> export(HttpServletResponse response, ApiSystemQueryRequest queryRequest,
                                    @RequestParam(value = "exportName", defaultValue = "接口业务系统", required = false) String exportName) throws IOException {

        ExcelUtils.export(response, apiSystemService.getList(queryRequest), ApiSystemListItemResponse.class, exportName, "接口业务系统");
        return null;
    }
}
